package com.czk.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.czk.dao.LogDateMapper;
import com.czk.dao.LogIdMapper;
import com.czk.pojo.LogDate;
import com.czk.pojo.LogId;
import com.czk.utils.IdGeneratorSnowflake;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/5/17 11:36
 */
@RestController
@RequestMapping("/shared")
public class TableSharedController {
    @Autowired
    private LogDateMapper logDateMapper;
    @Autowired
    private LogIdMapper logIdMapper;

    @GetMapping("/date")
    public String test1() {
        LogDate logDate = new LogDate();
        logDate.setId(new IdGeneratorSnowflake().snowflakeId());
        logDate.setComment("测试内容");
        logDate.setCreateDate(new Date());
        //插入
        logDateMapper.save(logDate);
        //查询
        List<LogDate> logDates = logDateMapper.queryList();
        Object json = JSONArray.toJSON(logDates);
        return json.toString();
    }

    @GetMapping("/id")
    public String test2() {
        LogId logId = new LogId();
        // 全是偶数
        long id = new IdGeneratorSnowflake().snowflakeId();
        logId.setId(id+1);
        logId.setComment("测试");
        logId.setCreateDate(new Date());
        //插入
        logIdMapper.save(logId);
        //查询
        LogId logIdObject = logIdMapper.queryOne(id+1);
        return JSON.toJSONString(logIdObject);
    }
}
